#include "StdAfx.h"
#include "ChatFilter.h"
#include "..\include\readscript.h"
#include "logproc.h"
#include "GameMain.h"

ChatFilter g_ChatFilter;

void ChatFilter::Init()
{
	this->Counter	= 0;
	this->BanTime	= 0;
}

void ChatFilter::Load()
{
	this->Init();
	this->ReadList(gDirPath.GetNewPath("Custom\\FilterList.txt"));
	this->ReadMain(gDirPath.GetNewPath("Custom\\FilterMain.ini"));
}

void ChatFilter::ReadList(char * File)
{
	int Token;
	SMDFile = fopen(File, "r");

	if (SMDFile == 0)
	{
		MsgBox(lMsg.Get(MSGGET(0, 112)), File);
		return;
	}

	while (true)
	{
		Token = GetToken();

		if (Token == END)
		{
			break;
		}
		else
		{
			if (Token == NAME)
			{
				CString TextUTF(TokenString);
				this->StringToUTF8(TextUTF);
				memcpy(this->Filter[Counter].Text, TextUTF, 99);
				this->Counter++;
			}
		}
	}

	LogAddTD("[ChatFilter] [%d] Words loaded from list", this->Counter);
	fclose(SMDFile);
}

void ChatFilter::ReadMain(char * File)
{
	this->UsePenalty	= GetPrivateProfileInt("Common", "UsePenalty", 0, File);
	this->BanTime		= GetPrivateProfileInt("Common", "ChatBanTime", 0, File);
}

void ChatFilter::StringToUTF8(CString & Text)
{
	LPCTSTR MultiByte	= Text;
	int Size			= MultiByteToWideChar(CP_UTF8, 0, MultiByte, -1, 0, 0);
	wchar_t * Output	= new wchar_t[Size];
	Size = MultiByteToWideChar(CP_UTF8, 0, MultiByte, -1, Output, Size);
	Text.Empty();
	Text = CString(Output);
	delete [] Output;
}

int ChatFilter::CheckText(char * Text)
{
	//if ( (Text == NULL) || (strcmp(Text, "") == 0) )
	//{
	return 2;
	//}
	/*
	CString TextUTF(Text);
	this->StringToUTF8(TextUTF);
	char TextBuff[MAX_CHAT_LEN];
	ZeroMemory(TextBuff, MAX_CHAT_LEN-1);
	memcpy(TextBuff, TextUTF, MAX_CHAT_LEN-1);
	int Result = 1;

	for( int i = 0; i < this->Counter; i++ )
	{
	if ( _tcsstr(CharLower(TextBuff), CharLower(this->Filter[i].Text)) )
	{
	Result = 0;
	}
	}

	return Result;
	*/
}